//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;

using ProjectBase.Core;

namespace $safeprojectname$
{
    /// <summary>
    /// Entity FieldObject.
    /// </summary>
    [Serializable]
    public class FieldObject : Persistent<int>, IField
    {
        private Type _type;
        private string _dataType;
        private string _subType;

        public virtual string PropertyName { get; set; }
        public virtual string ColumnName { get; set; }
        public virtual string TableName { get; set; }
        public virtual string DisplayName { get; set; }

        public virtual string DataType
        {
            get { return _dataType; }
            set
            {
                _dataType = value;
                if (_dataType.IsNotEmpty() && _dataType.StartsWith("CustomList."))
                {
                    _subType = _dataType.Split('.')[1];
                }
            }
        }

        public virtual string SubType { get { return _subType; } }

        public virtual Type Type
        {
            get
            {
                if (_type.IsNull())
                {
                    if (DataType.IsEmpty())
                    {
                        return null;
                    }

                    if (DataType.StartsWith("CustomList."))
                    {
                        _type = typeof(CustomList);
                        return _type;
                    }

                    switch (DataType.ToLower())
                    {
                        case "int":
                            _type = IsStatic && IsRequired ? typeof(int) : typeof(int?);
                            break;

                        case "integer":
                            _type = IsStatic && IsRequired ? typeof(int) : typeof(int?);
                            break;

                        case "int32":
                            _type = IsStatic && IsRequired ? typeof(int) : typeof(int?);
                            break;

                        case "bool":
                            _type = IsStatic && IsRequired ? typeof(bool) : typeof(bool?);
                            break;

                        case "boolean":
                            _type = IsStatic && IsRequired ? typeof(bool) : typeof(bool?);
                            break;

                        case "decimal":
                            _type = IsStatic && IsRequired ? typeof(decimal) : typeof(decimal?);
                            break;

                        case "string":
                            _type = typeof(string);
                            break;

                        case "datetime":
                            _type = IsStatic && IsRequired ? typeof(DateTime) : typeof(DateTime?);
                            break;

                        default:
                            _type = Type.GetType("$safeprojectname$." + DataType + ",$safeprojectname$");
                            break;
                    }
                }
                return _type;
            }
            set { _type = value; }
        }

        public virtual int? Length { get; set; }

        public virtual bool IsRequired { get; set; }
        public virtual bool IsUnique { get; set; }
        public virtual bool IsStatic { get; set; }

        public virtual string MinValue { get; set; }
        public virtual string MaxValue { get; set; }

        #region override abstract

        /// <summary>String representation of the 'IField' instance</summary>
        public override string ToDisplay()
        {
            return PropertyName;
        }

        /// <summary> Returned value should be business unique key</summary>
        protected override string GetDomainObjectSignature()
        {
            return PropertyName;
        }

        #endregion override abstract
    }
}